﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

public class PuzzleObject : Objects
{
    private Direction direction;
    public Direction Direction
    {
        get { return direction; }
        set { direction = value; }
    }

    private bool intouch;
    public bool inTouch
    {
        get { return intouch; }
        set
        {
            if (!intouch && value)
            {
                State = !State;
            }
            intouch = value;
        }
    }

    protected Texture2D testCollider;

    protected bool state;
    public bool State
    {
        set 
        {
            if (value != state)
            {
                
                if (kind == ObjectKind.TouchingButton)
                {
                    Activate();
                }
                state = value;
                if (state && kind == ObjectKind.WallUp)
                {
                    GameSession.CollisionMatriz[(int)xposition, (int)yposition] = 0;
                }
                if (!state && kind == ObjectKind.WallUp)
                {
                    GameSession.CollisionMatriz[(int)xposition, (int)yposition] = 2;
                }
            }
        }
        get { return state; }
    }
    
    private int paintNumber;
    public int PaintNumber
    {
        get
        {
            if (state && kind == ObjectKind.WallUp)
                return (int)kind + 1;
            else
                if (state && kind == ObjectKind.TouchingButton)
                    return (int)kind + 4;
                else
                    return (int)kind;
        }
    }

    private List<PuzzleGroup> groupList;

    public PuzzleObject(ObjectKind kind, int xposition, int yposition, bool state)
        :base(kind, xposition,yposition)
    {
        groupList = new List<PuzzleGroup>();

        if (kind == ObjectKind.WallUp || kind == ObjectKind.PushingStone || kind == ObjectKind.Tower)
            this.canCollide = true;
        else
            this.canCollide = false;

        this.State = state;

        testCollider = GameSession.Content.Load<Texture2D>("Textures/Characters/testCollider");

        intouch = false;
    }

    private void Activate()
    {
        foreach (PuzzleGroup group in groupList)
        {
            foreach (PuzzleObject obj in group.ObjectList)
            {
                if (obj.Kind == ObjectKind.WallUp || obj.Kind == ObjectKind.WallDown)
                {
                    obj.State = !obj.State;
                    if(!obj.state)
                        GameSession.CheckPlayerPosition((int)obj.XPosition, (int)obj.YPosition);
                }
            }
        }
    }

    internal void AddGroup(PuzzleGroup group)
    {
        groupList.Add(group);
    }

    public bool hasMoved = false;

    public void Update(GameTime gameTime)
    {
        if (kind == ObjectKind.PushingStone && state)
        {
            //mover
            switch (direction)
            {
                case Direction.Down:
                    {
                        if (GameSession.CollisionMatriz[(int)xposition, (int)yposition + 1] == 0
                            || (GameSession.CollisionMatriz[(int)xposition, (int)yposition + 1] == 2 && !GameSession.CanCollide((int)xposition, (int)yposition + 1)))
                        {
                            state = true;
                            YPosition++;
                            hasMoved = true;
                        }
                        else
                            state = false;
                        break;
                    }
                case Direction.Up:
                    {
                        if (GameSession.CollisionMatriz[(int)xposition, (int)yposition - 1] == 0
                            || (GameSession.CollisionMatriz[(int)xposition, (int)yposition - 1] == 2 && !GameSession.CanCollide((int)xposition, (int)yposition - 1)))
                        {
                            state = true;
                            YPosition--;
                            hasMoved = true;
                        }
                        else
                            state = false;
                        break;
                    }
                case Direction.Left:
                    {
                        if (GameSession.CollisionMatriz[(int)xposition - 1, (int)yposition] == 0
                            || (GameSession.CollisionMatriz[(int)xposition - 1, (int)yposition] == 2 && !GameSession.CanCollide((int)xposition - 1, (int)yposition)))
                        {
                            state = true;
                            XPosition--;
                            hasMoved = true;
                        }
                        else
                            state = false;
                        break;
                    }
                case Direction.Right:
                    {
                        if (GameSession.CollisionMatriz[(int)xposition + 1, (int)yposition] == 0
                            || (GameSession.CollisionMatriz[(int)xposition + 1, (int)yposition] == 2 && !GameSession.CanCollide((int)xposition + 1, (int)yposition)))
                        {
                            state = true;
                            XPosition++;
                            hasMoved = true;
                        }
                        else
                            state = false;
                        break;
                    }
            }

            if (!state && hasMoved)
            {
                AudioManager.PlaySound(@"Audio\Puzzle\Bloco batendo na parede");
                hasMoved = false;
            }

            Characters en = null;

            foreach (Characters chara in GameSession.CharactersList)
            {
                if (chara.State == Characters.States.Alive)
                {
                    if (chara.GetType() != typeof(KeiJow)
                        && chara.GetType() != typeof(Manteiga))
                    {
                        if (chara.BoundingRectangle.Intersects(new Rectangle((int)xposition * 64, (int)yposition * 64, 64, 64)))
                        {
                            //en = chara;
                            chara.TakeDamage(5);
                            AudioManager.PlaySound(@"Audio\Puzzle\Bloco batendo inimigo");
                        }
                    }
                    else
                    {
                        if (chara.BoundingRectangle.Intersects(new Rectangle((int)xposition * 64, (int)yposition * 64, 64, 64)))
                        {
                            chara.TakeDamage(1);
                        }
                    }
                }
            }

            //GameSession.CharactersList.Remove(en);

        }
        if (kind == ObjectKind.TouchingButton)
        {

            bool intouchlocal = false;
            foreach (Characters chara in GameSession.CharactersList)
            {
                if (chara.GetType() == typeof(KeiJow)
                        || chara.GetType() == typeof(Manteiga))
                {
                    if (chara.BoundingRectangle.Intersects(new Rectangle((int)xposition * 64, (int)yposition * 64, 64, 64)))
                    {
                        intouchlocal = true;
                        inTouch = true;
                        break;
                    }
                }
            }
            if (!intouchlocal)
                inTouch = false;
        }

    }

    public void Draw(int xtile, int ytile, SpriteBatch spriteBatch)
    {
        if (kind == ObjectKind.PushingStone)
        {

            spriteBatch.Draw(GameSession.Objects, new Vector2(XPosition * 64, YPosition * 64 + GameSession.screenAdjustment), 
                new Rectangle(xtile * 64, ytile * 64, 64, 64), Color.White);

            //spriteBatch.Draw(testCollider, new Vector2(xposition * 64, yposition * 64 + GameSession.screenAdjustment),
            //    new Rectangle(0, 0, 64, 64), Color.White);

        }
    }
}
